package com.ta.controller;

import com.ta.service.ex.InsertException;
import com.ta.service.ex.ServiceException;
import com.ta.service.ex.UserNameRepeatException;
import com.ta.util.JsonResult;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;


/**
 * 统一异常处理
 */
public class BaseController {

    @ExceptionHandler(ServiceException.class)
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> result = new JsonResult<>(e);
        return result;
    }

    public Integer getUid(HttpSession session){
       return Integer.valueOf(session.getAttribute("uid").toString());
    }

    public String getUserName(HttpSession session){
       return session.getAttribute("userName").toString();
    }

    public String getAvatar(HttpSession session){
       return session.getAttribute("avatar").toString();
    }

    public Integer getOid(HttpSession session){
       return Integer.valueOf(session.getAttribute("oid").toString());
    }

    public Long getTotalPrice(HttpSession session){
       return Long.valueOf(session.getAttribute("totalPrice").toString());
    }
}
